package com.net.rip.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 网络拓扑实体类
 */
public class Network {

    private int routerNum;                //路由器个数
    private List<Router> routerList;    //路由器集合

    public int getRouterNum() {
        return routerNum;
    }

    public void setRouterNum(int routerNum) {
        this.routerNum = routerNum;
    }


    public List<Router> getRouterList() {
        return routerList;
    }

    public void setRouterList(List<Router> routerList) {
        this.routerList = routerList;
    }

    //初始化路由表
    public void initRouter() {
        routerNum = 6;
        this.routerList = new ArrayList<Router>();

        Map<String, Information> informationA = new HashMap<>();
        informationA.put("N1", new Information("N1", 1, "null"));
        informationA.put("N2", new Information("N2", 1, "null"));
        informationA.put("N3", new Information("N3", 1, "null"));
        this.routerList.add(new Router("A", informationA));

        Map<String, Information> informationB = new HashMap<>();
        informationB.put("N3", new Information("N3", 1, "null"));
        informationB.put("N4", new Information("N4", 1, "null"));
        this.routerList.add(new Router("B", informationB));

        Map<String, Information> informationC = new HashMap<>();
        informationC.put("N4", new Information("N4", 1, "null"));
        informationC.put("N5", new Information("N5", 1, "null"));
        this.routerList.add(new Router("C", informationC));

        Map<String, Information> informationD = new HashMap<>();
        informationD.put("N2", new Information("N2", 1, "null"));
        informationD.put("N6", new Information("N6", 1, "null"));
        this.routerList.add(new Router("D", informationD));

        Map<String, Information> informationE = new HashMap<>();
        informationE.put("N1", new Information("N1", 1, "null"));
        informationE.put("N6", new Information("N6", 1, "null"));
        this.routerList.add(new Router("E", informationE));

        Map<String, Information> informationF = new HashMap<>();
        informationF.put("N5", new Information("N5", 1, "null"));
        informationF.put("N6", new Information("N6", 1, "null"));
        this.routerList.add(new Router("F", informationF));

    }

    //初始化相邻路由
    public void initNearRouter() {
        /**
         * 相邻路由器会有一个共同的目的网络
         * 从当前路由器出发，查询路由表中有相同的目的网络的路由器加入当前路由器
         */
        for (int i = 0; i < this.routerNum; i++) {
            Set<String> set = this.routerList.get(i).getInformation().keySet();
            List<Router> nearRouter = new ArrayList<Router>();
            for (int j = 0; j < this.routerNum; j++) {
                if (i != j) {
                    for (String s : set) {
                        if (this.routerList.get(j).getInformation().containsKey(s))
                            nearRouter.add(this.routerList.get(j));
                    }
                }
            }
            this.routerList.get(i).setNearRouter(nearRouter);
        }
    }

    //初始化网络拓扑
    public void initNetwork() {
        initRouter();
        initNearRouter();
    }

}

